﻿using IOA.MES.EsbApi.Models.Response;
using IOA.MES.EsbApi.Models.Request;
using NLog;
using System.Web.Http;
using IOA.MES.EsbApi.Models.Request.Allocate;
using IOA.MES.DataAccess.VModel;
using System.Linq;
using IOA.MES.Business;
using System.Collections.Generic;

namespace IOA.MES.EsbApi.Controllers
{
    /// <summary>
    /// 调拨管理
    /// </summary>
    public class AllocateController : ApiController
    {
        private Logger logger = LogManager.GetLogger(nameof(AllocateController));

        /// <summary>
        /// 创建调拨单
        /// </summary>
        [HttpPost]
        public BaseResponse CreateAllocateOrder([FromBody] BaseRequest<CreateAllocateOrderRequest> request)
        {
            if (request == null || request.requestInfo == null || request.requestInfo.data == null || request.requestInfo.data.list == null)
            {
                return new BaseResponse { esbInfo = new EsbResponseInfo("1", "E", "报文为空") };
            }

            var models = new[] { request.requestInfo.data.list }.Select(_ => new VAllocateOrder
            {
                AllocateNo = _.DocumentNumber?.ToUpper().Trim(),
                WorkOrderNo = _.WipEntityName?.ToUpper().Trim(),
                FromWarehouseCode = _.ApplyLines?.FirstOrDefault()?.SourceSubinv?.ToUpper().Trim(),
                ToWarehouseCode = _.ApplyLines?.FirstOrDefault()?.DestSubinv?.ToUpper().Trim(),
                Products = _.ApplyLines.Select(p => new VAllocateProduct
                {
                    AllocateNo = _.DocumentNumber?.ToUpper().Trim(),
                    ProductCode = p.ItemNumber?.ToUpper().Trim(),
                    Quantity = p.PrimaryQuantity,
                    Unit = p.PrimaryUnitOfMeasure?.ToUpper().Trim(),
                    BatchNo = p.LotNumber?.ToUpper().Trim(),
                    ProductionCompany = p.Manufactures?.ToUpper().Trim()
                }).ToList()
            }).ToList();
            new AllocateOrderMgr().CreateAllocateOrder(models);
            return new BaseResponse();
        }

        /// <summary>
        /// 同步搬运单
        /// </summary>
        [HttpPost]
        public BaseResponse CreateMovingOrder([FromBody] BaseRequest<CreateMovingOrderRequest> request)
        {
            if (request == null || request.requestInfo == null || request.requestInfo.data == null || request.requestInfo.data.list == null)
            {
                return new BaseResponse { esbInfo = new EsbResponseInfo("1", "E", "报文为空") };
            }

            var models = new[] { request.requestInfo.data.list }.Select(_ => new VAllocateOrder
            {
                AllocateNo = _.RequestId?.ToUpper().Trim(),
                WorkOrderNo = _.Lines?.FirstOrDefault()?.OrderId?.Trim(),
                Products = (_.Lines ?? new List<MoveOrderItem>()).Select(p => new VAllocateProduct
                {
                    AllocateNo = _.RequestId?.ToUpper().Trim(),
                    ProductCode = p.MaterialNo?.ToUpper().Trim(),
                    ProductionCompany = p.DetailAttribute5?.ToUpper().Trim(),
                    Quantity = p.IssueQty,
                    Unit = p.Unit?.ToUpper().Trim(),
                    BatchNo = p.DetailAttribute6?.ToUpper().Trim(),
                    BatchNoBig = p.BatchNoBig?.ToUpper()?.Trim(),
                    BatchNoSmall = p.BatchNoSmall?.ToUpper()?.Trim()
                }).ToList()
            }).ToList();
            new AllocateOrderMgr().AsyncMoveOrder(models);
            return new BaseResponse();
        }
    }
}
